package org.fireflow2.designer.eclipse.properties.tab;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow.pdl.fpdl20.process.SubProcess;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;

public class WorkflowProcessAttributePropertySection extends
		AbstractPropertySection {
	AbstractEditPart editPart = null;
	AbsWrapper modelWrapper = null;
	protected Text taskInstanceCreator = null;
	
    protected Text formTaskInstanceRunner = null;

    protected Text toolTaskInstanceRunner = null;

    protected Text subflowTaskInstanceRunner = null;
    
    protected Text formTaskInstanceCompletionEvaluator = null;

    protected Text toolTaskInstanceCompletionEvaluator = null;

    protected Text subflowTaskInstanceCompletionEvaluator = null;    
	
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		Composite composite = getWidgetFactory()
				.createFlatFormComposite(parent);

		composite.setLayout(new GridLayout(2, false));
		
		// Task instance creator 
		getWidgetFactory().createCLabel(composite, "Task Instance Creator: ");
		
		taskInstanceCreator  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		taskInstanceCreator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		taskInstanceCreator.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				Subflow process = (Subflow)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getTaskInstanceCreator();
//				if (oldValue==null) oldValue="";
//				String newValue = taskInstanceCreator.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setTaskInstanceCreator(newValue.trim());
			}

		});
		
		// Form Task Instance Runner 
		getWidgetFactory().createCLabel(composite, "Form Task Instance Runner: ");
		
		formTaskInstanceRunner  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		formTaskInstanceRunner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		formTaskInstanceRunner.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				WorkflowProcess process = (WorkflowProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getFormTaskInstanceRunner();
//				if (oldValue==null) oldValue="";
//				String newValue = formTaskInstanceRunner.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setFormTaskInstanceRunner(newValue.trim());
			}

		});		
		
		// Tool Task instance runner 
		getWidgetFactory().createCLabel(composite, "Tool Task Instance Runner: ");
		
		toolTaskInstanceRunner  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		toolTaskInstanceRunner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		toolTaskInstanceRunner.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				WorkflowProcess process = (WorkflowProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getToolTaskInstanceRunner();
//				if (oldValue==null) oldValue="";
//				String newValue = toolTaskInstanceRunner.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setToolTaskInstanceRunner(newValue.trim());
			}

		});			
		
		// Subflow Task instance runner 
		getWidgetFactory().createCLabel(composite, "Subflow Task Instance Runner: ");
		
		subflowTaskInstanceRunner  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		subflowTaskInstanceRunner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		subflowTaskInstanceRunner.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				WorkflowProcess process = (WorkflowProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getSubflowTaskInstanceRunner();
//				if (oldValue==null) oldValue="";
//				String newValue = subflowTaskInstanceRunner.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setSubflowTaskInstanceRunner(newValue.trim());
			}

		});			
		
		// Form Task instance completion evaluator 
		getWidgetFactory().createCLabel(composite, "Form Task Instance Completion Evaluator: ");
		
		formTaskInstanceCompletionEvaluator  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		formTaskInstanceCompletionEvaluator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		formTaskInstanceCompletionEvaluator.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				WorkflowProcess process = (WorkflowProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getFormTaskInstanceCompletionEvaluator();
//				if (oldValue==null) oldValue="";
//				String newValue = formTaskInstanceCompletionEvaluator.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setFormTaskInstanceCompletionEvaluator(newValue.trim());
			}

		});					
		
		// Tool Task instance completion evaluator 
		getWidgetFactory().createCLabel(composite, "Tool Task Instance Completion Evaluator: ");
		
		toolTaskInstanceCompletionEvaluator  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		toolTaskInstanceCompletionEvaluator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		toolTaskInstanceCompletionEvaluator.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				WorkflowProcess process = (WorkflowProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getToolTaskInstanceCompletionEvaluator();
//				if (oldValue==null) oldValue="";
//				String newValue = toolTaskInstanceCompletionEvaluator.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setToolTaskInstanceCompletionEvaluator(newValue.trim());
			}

		});			
		
		// Subflow Task instance completion evaluator 
		getWidgetFactory().createCLabel(composite, "Subflow Task Instance Completion Evaluator: ");
		
		subflowTaskInstanceCompletionEvaluator  = this.getWidgetFactory().createText(composite, "");//,SWT.SINGLE|SWT.BORDER);
		subflowTaskInstanceCompletionEvaluator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		subflowTaskInstanceCompletionEvaluator.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
//				WorkflowProcess process = (WorkflowProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
//				String oldValue = process.getSubflowTaskInstanceCompletionEvaluator();
//				if (oldValue==null) oldValue="";
//				String newValue = subflowTaskInstanceCompletionEvaluator.getText();
//				if (newValue==null) newValue="";
//				if (oldValue.trim().equals(newValue.trim()))return;
//				
//				((SubProcessWrapper)modelWrapper).setSubflowTaskInstanceCompletionEvaluator(newValue.trim());
			}

		});				
	}
	
	@Override
	public void refresh() {
		super.refresh();
		SubProcess process = (SubProcess)((SubProcessWrapper)modelWrapper).getWorkflowModelElement();
		String taskInstanceCreatorStr = "";//process.getTaskInstanceCreator();
		if(taskInstanceCreatorStr!=null && !taskInstanceCreatorStr.trim().equals("")){
			taskInstanceCreator.setText(taskInstanceCreatorStr);
		}else{
			taskInstanceCreator.setText("");
		}
		
	    String formTaskInstanceRunnerStr = "";//process.getFormTaskInstanceRunner();
		if(formTaskInstanceRunnerStr!=null && !formTaskInstanceRunnerStr.trim().equals("")){
			formTaskInstanceRunner.setText(formTaskInstanceRunnerStr);
		}else{
			formTaskInstanceRunner.setText("");
		}
			    

	    String toolTaskInstanceRunnerStr = "";//process.getToolTaskInstanceRunner();
		if(toolTaskInstanceRunnerStr!=null && !toolTaskInstanceRunnerStr.trim().equals("")){
			toolTaskInstanceRunner.setText(toolTaskInstanceRunnerStr);
		}else{
			toolTaskInstanceRunner.setText("");
		}
		
		
	    String subflowTaskInstanceRunnerStr = "";//process.getSubflowTaskInstanceRunner();
		if(subflowTaskInstanceRunnerStr!=null && !subflowTaskInstanceRunnerStr.trim().equals("")){
			subflowTaskInstanceRunner.setText(subflowTaskInstanceRunnerStr);
		}else{
			subflowTaskInstanceRunner.setText("");
		}	    
	    
	    String formTaskInstanceCompletionEvaluatorStr = "";//process.getFormTaskInstanceCompletionEvaluator();
		if(formTaskInstanceCompletionEvaluatorStr!=null && !formTaskInstanceCompletionEvaluatorStr.trim().equals("")){
			formTaskInstanceCompletionEvaluator.setText(formTaskInstanceCompletionEvaluatorStr);
		}else{
			formTaskInstanceCompletionEvaluator.setText("");
		}	
		
	    String toolTaskInstanceCompletionEvaluatorStr = "";//process.getToolTaskInstanceCompletionEvaluator();
		if(toolTaskInstanceCompletionEvaluatorStr!=null && !toolTaskInstanceCompletionEvaluatorStr.trim().equals("")){
			toolTaskInstanceCompletionEvaluator.setText(toolTaskInstanceCompletionEvaluatorStr);
		}else{
			toolTaskInstanceCompletionEvaluator.setText("");
		}	
	    
	    String subflowTaskInstanceCompletionEvaluatorStr ="";// process.getSubflowTaskInstanceCompletionEvaluator();
		if(subflowTaskInstanceCompletionEvaluatorStr!=null && !subflowTaskInstanceCompletionEvaluatorStr.trim().equals("")){
			subflowTaskInstanceCompletionEvaluator.setText(subflowTaskInstanceCompletionEvaluatorStr);
		}else{
			subflowTaskInstanceCompletionEvaluator.setText("");
		}		    
	}	
	
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object obj = ((IStructuredSelection) selection).getFirstElement();
		if (obj instanceof AbstractEditPart) {
			editPart = (AbstractEditPart) obj;
			modelWrapper = (AbsWrapper) editPart.getModel();
		}
	}		
}
